import { defineStore } from "pinia";
import router from "@/router";
class adminInfo {
  id: number;
  login_time: number;
  name: string;
  role_id: number;
  user: string;
  constructor() {
    this.id = -1;
    this.login_time = 0;
    this.name = "";
    this.role_id = -1;
    this.user = "";
  }
}

//管理员信息
export const useAdminStore = defineStore("admin", () => {
  let adminObj = new adminInfo();
  let tokenStr = "";
  if (localStorage.getItem("admin") && localStorage.getItem("token")) {
    adminObj = JSON.parse(localStorage.getItem("admin") as string);
    tokenStr = localStorage.getItem("token") as string;
  }
  const info = ref<adminInfo>(adminObj);
  const token = ref<string>(tokenStr);

  //退出登录
  function loginOut() {
    localStorage.removeItem("admin");
    localStorage.removeItem("token");
    info.value = new adminInfo();
    token.value = "";
    router.push("/login");
  }

  return { info, token, loginOut };
});
